<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <div slot="detail">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="12">
              <a-form-model-item label="轮播图名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
                <a-input v-model="model.name" placeholder="轮播图名称"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="轮播图(2:1)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="images">
                <image-upload-sort biz-path="laile2/serve" isMultiple :number="9" v-model="model.imagesCarousel" @change="handleChange"></image-upload-sort>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row v-show="model.imagesCarousel && model.imagesCarousel.length > 0">
            <a-divider orientation="left" style="padding-bottom: 12px;">轮播图预览</a-divider>
            <a-carousel arrows>
              <div slot="prevArrow" class="custom-slick-arrow" style="left: 10px; zIndex: 1">
                <a-icon type="left-circle" />
              </div>
              <div slot="nextArrow" class="custom-slick-arrow" style="right: 10px">
                <a-icon type="right-circle" />
              </div>
              <div v-for="(data, index) in model.imagesCarousel" :key="index" class="demoCarousel">
                <img :src="data.replace('http://', 'https://')" preview="index" :alt="data">
              </div>
            </a-carousel>
          </a-row>
        </a-form-model>
      </div>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import ImageUploadSort from './ImageUploadSort'

export default {
  name: 'ImagesCarouselForm',
  components: {
    ImageUploadSort
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
  },
  data() {
    return {
      model: {
      },
      map: {
        "http://img.caocaog.com/laile2/serve/304c623378344a493276_1640754508298.jpg": 0,
        "http://img.caocaog.com/laile2/serve/cacochan_1640754510249.png": 1,
        "http://img.caocaog.com/laile2/serve/Dt5uRWiVAAIKn2X_1640757298707.jpg": 2
      },
      labelCol: {
        xs: { span: 25 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 15 },
        sm: { span: 10 },
      },
      confirmLoading: false,
      validatorRules: {
        type: [{
          required: true, message: '请输入轮播图类型'
        }],
        name: [
          { required: true, message: '请输入轮播图名称' },
        ]
      },
      url: {
        add: "/imagesCarousel",
        edit: "/imagesCarousel",
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    handleChange(e) {

    },
    add() {
      this.visible = true;
    },
    edit(record) {
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      let imagesCarousel = []
      console.log(this.model.imagesCarousel)
      if (this.model.imagesCarousel.length === 0)
        return this.$message.warning('请至少传入一张图片')
      else if (this.model.imagesCarousel) {
        if (typeof this.model.imagesCarousel === 'string') {
          imagesCarousel = this.model.imagesCarousel.split(',')
        } else {
          imagesCarousel = this.model.imagesCarousel
        }
      }
      console.log()
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          this.model.positionType = 7
          this.model.createTime = null
          this.model.updateTime = null
          httpAction(httpurl, { ...this.model, imagesCarousel }, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>
<style scoped>
.demoCarousel {
  width: 100%;
  height: 250px;
}
.demoCarousel img {
  width: 100%;
  height: 100%;
}
/* For demo */
.ant-carousel >>> .slick-slide {
  text-align: center;
  height: 250px;
  line-height: 250px;
  background: #364d79;
  overflow: hidden;
}

.ant-carousel >>> .custom-slick-arrow {
  width: 25px;
  height: 25px;
  font-size: 25px;
  color: #fff;
  background-color: rgba(31, 45, 61, 0.11);
  opacity: 0.3;
}
.ant-carousel >>> .custom-slick-arrow:before {
  display: none;
}
.ant-carousel >>> .custom-slick-arrow:hover {
  opacity: 0.5;
}

.ant-carousel >>> .slick-slide h3 {
  color: #fff;
}
</style>